package com.pd.modules.client.dialogs;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import com.pd.modules.client.Client;
import com.pd.modules.client.database.DBReservationsHandler;
import com.pd.modules.client.database.Database;
import com.pd.modules.common.DateInterval;

@SuppressWarnings("serial")
public class DialogCreateReservation extends JDialog
{

	private final JPanel contentPanel = new JPanel();

	private JFormattedTextField mTxtDate;
	private JFormattedTextField mTxtStartTime;
	private JFormattedTextField mTxtEndTime;
	private JLabel lblData;
	private JLabel lblHoraDeInicio;
	private JLabel lblHoraDeFim;
	private JButton okButton;
	private JButton cancelButton;
	private String mRoomID;
	

	/**
	 * Create the dialog.
	 */
	public DialogCreateReservation(String _roomID)
	{
		setModal(true);
		setResizable(false);
		mRoomID = _roomID;
		setTitle("Nova reserva para a sala: " + mRoomID);

		setModalityType(ModalityType.APPLICATION_MODAL);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

		setBounds(100, 100, 350, 130);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);

		mTxtDate = new JFormattedTextField(new SimpleDateFormat("dd/MM/yyyy"));
		mTxtDate.setBounds(104, 17, 102, 20);
		mTxtDate.setHorizontalAlignment(SwingConstants.CENTER);
		mTxtDate.setValue(Calendar.getInstance().getTime());
		mTxtStartTime = new JFormattedTextField("HH:mm");
		mTxtStartTime.setBounds(103, 46, 103, 20);
		mTxtStartTime.setHorizontalAlignment(SwingConstants.CENTER);
		mTxtStartTime.setValue("08:30");
		mTxtEndTime = new JFormattedTextField("HH:mm");
		mTxtEndTime.setBounds(103, 77, 103, 20);
		mTxtEndTime.setHorizontalAlignment(SwingConstants.CENTER);
		mTxtEndTime.setValue("09:30");

		lblData = new JLabel("Data:");
		lblData.setBounds(15, 20, 36, 14);
		lblHoraDeInicio = new JLabel("Hora de Inicio:");
		lblHoraDeInicio.setBounds(15, 48, 86, 14);
		lblHoraDeFim = new JLabel("Hora de Fim:");
		lblHoraDeFim.setBounds(15, 80, 78, 14);
		{
			okButton = new JButton("Criar");
			okButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{

					// Dados das textbox's
					String txtDay = mTxtDate.getText();
					String txtStartHour = mTxtStartTime.getText();
					String txtEndHour = mTxtEndTime.getText();

					try
					{
						DateInterval interval = new DateInterval(txtDay, txtStartHour, txtEndHour);

						// Tenta criar a reserva
						String username = Client.getInstance().getUsername();
						DBReservationsHandler reservations = Database.getInstance().Reservations();
						
						reservations.createRoomReservation(mRoomID, interval,username);

						setVisible(false);
					} catch (Exception e1)
					{
						JOptionPane.showMessageDialog(null, e1.getMessage());
					}
				}
			});
			okButton.setBounds(253, 11, 86, 23);
			okButton.setActionCommand("OK");
			getRootPane().setDefaultButton(okButton);
		}
		{
			cancelButton = new JButton("Cancelar");
			cancelButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent arg0)
				{
					setVisible(false);
				}
			});
			cancelButton.setBounds(253, 44, 86, 23);
			cancelButton.setActionCommand("Cancel");
		}
		contentPanel.setLayout(null);
		contentPanel.add(lblHoraDeFim);
		contentPanel.add(mTxtStartTime);
		contentPanel.add(lblData);
		contentPanel.add(lblHoraDeInicio);
		contentPanel.add(mTxtEndTime);
		contentPanel.add(mTxtDate);
		contentPanel.add(cancelButton);
		contentPanel.add(okButton);
	}
}
